import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=[8, 8])
spr = 1
spc = 1
spn = 0

# load
from sklearn.datasets import load_breast_cancer
x, y = load_breast_cancer(return_X_y=True)

# scale
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
pipe = Pipeline([
    ['std', StandardScaler()]
])
x = pipe.fit_transform(x)

# split
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=666)

# grid search
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
fixed_params = dict(solver='liblinear',
                    max_iter=500,
                    )
estimator = LogisticRegression(**fixed_params)
params = dict(penalty=['l1', 'l2'],
              C=[0.1, 0.25, 0.5, 0.75, 1, 1.5, 2, 3],
              )
grid = GridSearchCV(estimator, params, cv=5)
grid.fit(x_train, y_train)
print(f'Best score = {grid.best_score_}')
print(f'Best params = {grid.best_params_}')

# model
clf = LogisticRegression(**fixed_params,
                         **(grid.best_params_))
clf.fit(x_train, y_train)
print(f'Training score = {clf.score(x_train, y_train)}')
print(f'Testing score = {clf.score(x_test, y_test)}')

# learning curve
spn += 1
plt.subplot(spr, spc, spn)
from sklearn.model_selection import learning_curve
xlist = np.array(np.linspace(0.1, 1, 6))
train_size, train_score, test_score = learning_curve(clf, x, y, train_sizes=xlist, cv=5)
train_score_m = train_score.mean(axis=1)
test_score_m = test_score.mean(axis=1)
train_score_s = train_score.std(axis=1)
test_score_s = test_score.std(axis=1)
plt.plot(train_size, train_score_m, linestyle='-', marker='o', c='b', label='train')
plt.plot(train_size, test_score_m, linestyle='-', marker='o', c='r', zorder=100, label='test')
plt.fill_between(train_size, train_score_m - train_score_s, train_score_m + train_score_s, color='b', alpha=0.2, label='train')
plt.fill_between(train_size, test_score_m - test_score_s, test_score_m + test_score_s, color='r', alpha=0.2, label='test')
plt.legend()

# Finally show all drawings.
plt.show()
